package pt.ist.meic.pava.JVSTMSync;

import jvstm.Transaction;

import pt.ist.meic.pava.SyncCommons.*;

public aspect Transact perthis(withAtomic()){

	
	
	
	/**
	 * Any method call marked as Atomic
	 */
	pointcut withAtomic(): 
		call(@Atomic * *.*(..)) && !cflowbelow(call(@Atomic * *.*(..)));
	
	
	
	Object around(): withAtomic() {
		
		boolean finished;
		Object ret;
		
		while(true) {
			Transaction.begin();
			finished = false;
			
			try {
					
				ret = proceed();
				Transaction.commit();
				finished = true;
				return ret;
				
			}
			catch(jvstm.CommitException ce) {
				
				Transaction.abort();
				finished = true;
				
			}
			finally {
				if(!finished)
					Transaction.abort();
				
			}
			
		}
		
		
	}
	
	
	
	
}
